package com.smxy.mall.controller;

import com.smxy.mall.annotation.CurrentUser;
import com.smxy.mall.annotation.JwtIgnore;
import com.smxy.mall.common.Response;
import com.smxy.mall.model.Audience;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.user.AccountReq;
import com.smxy.mall.model.request.user.DetailReq;
import com.smxy.mall.model.request.user.PageListReq;
import com.smxy.mall.model.request.user.PhoneReq;
import com.smxy.mall.utils.JwtTokenUtil;
import com.smxy.mall.entity.User;
import com.smxy.mall.service.UserService;
import com.smxy.mall.utils.Token;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Autowired
    private Audience audience;

    @ApiOperation("账号注册")
    @JwtIgnore
    @PostMapping("/register/userName")
    public Object userNameReg(@Valid AccountReq accountReq) {
        return Response.success(userService.saveFromUserName(accountReq.getUserName(),accountReq.getUserPsw(),accountReq.getRolesId()));
    }

    @ApiOperation("账号登录")
    @JwtIgnore
    @PostMapping("/login/username")
    public Object usernameLogin(@Valid AccountReq accountReq){
        User result = userService.findByUserNameAndUserPsw(accountReq.getUserName(),accountReq.getUserPsw());
        if(StringUtils.isEmpty(result) || !result.getType().equals("admin")){
            return Response.fail("用户名或密码错误");
        }
        String token = JwtTokenUtil.createJWT(result, audience);
        Map<String,Object> map = new HashMap();
        map.put("token",token);
        map.put("userInfo",result);
        return Response.success(map);
    }

    @ApiOperation("手机号注册")
    @JwtIgnore  //忽然jwt拦截器
    @PostMapping("/register/phone")
    public Object phoneReg(@Valid PhoneReq phoneReq){
        return Response.success(userService.saveFromPhone(phoneReq.getPhone(),phoneReq.getUserPsw()));
    }

    @ApiOperation("手机号登录")
    @JwtIgnore
    @PostMapping("/login/phone")
    public Object phoneLogin(@Valid PhoneReq phoneReq) {
        User obj = userService.findByPhoneAndUserPsw(phoneReq.getPhone(),phoneReq.getUserPsw());
        if(StringUtils.isEmpty(obj)){
            return Response.fail("手机号或密码错误");
        }else{
            String token = JwtTokenUtil.createJWT(obj,audience); //创建token
            Map<String,Object> map = new HashMap();
            map.put("token",token);
            map.put("userInfo",obj);
            //返回token和用户信息
            return Response.success(map);
        }
    }

    @GetMapping(value = "/list")
    @ApiOperation("用户列表")
    public Object list(@Valid PageListReq pageListReq) {
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of((pageListReq.getPage()-1),pageListReq.getSize(),sort);
        Page<User> list;
        if(StringUtils.isEmpty(pageListReq.getType())){
            list = userService.list(pageable);
        }else{
            list = userService.listByType(pageListReq.getType(),pageable);
        }
        return Response.success(list);
    }

    @ApiOperation("用户个人信息")
    @GetMapping("/userInfo")
    public Object userInfo(@CurrentUser Current current) {
        return Response.success(userService.findById(current.getUserId()));
    }

    @ApiOperation("根据id获取用户信息")
    @GetMapping("/getUserInfo")
    public Object getUserInfo(@RequestParam("id")Integer id) {
        return Response.success(userService.findById(id));
    }

    @ApiOperation("修改用户权限")
    @PostMapping("/updateRoles")
    public Object updateRoles(@RequestParam("roles")String roles,@RequestParam("id")Integer id){
        return Response.success(userService.updateRoles(roles,id));
    }

    @ApiOperation("个人信息/订单角标/优惠券角标")
    @GetMapping("/userCenter")
    public Object userCenter(@CurrentUser Current current) {
        return Response.success(userService.userCenter(current.getUserId()));
    }

    @ApiOperation("获取当前用户路由")
    @JwtIgnore
    @GetMapping("/routes")
    public Object routes() {
        return Response.success(userService.getRoutes(1));
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/edit")
    public Object edit(@Valid DetailReq detailReq) {
        return Response.success(userService.edit(detailReq));
    }
}
